package com.ai.imagetext.repository;

import com.ai.imagetext.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    
    /**
     * 根据用户名查找用户
     */
    Optional<User> findByUsernameAndIsDeletedFalse(String username);
    
    /**
     * 根据邮箱查找用户
     */
    Optional<User> findByEmailAndIsDeletedFalse(String email);
    
    /**
     * 根据用户名或邮箱查找用户
     */
    @Query("SELECT u FROM User u WHERE (u.username = :usernameOrEmail OR u.email = :usernameOrEmail) AND u.isDeleted = false")
    Optional<User> findByUsernameOrEmailAndIsDeletedFalse(@Param("usernameOrEmail") String usernameOrEmail);
    
    /**
     * 检查用户名是否存在
     */
    boolean existsByUsernameAndIsDeletedFalse(String username);
    
    /**
     * 检查邮箱是否存在
     */
    boolean existsByEmailAndIsDeletedFalse(String email);
}